﻿using System;
using System.Collections;
using System.Collections.Specialized;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;

namespace VitCode.Castle.DataAccess
{
    public class CastleAccess : BaseDataAccess
    {
        /// <summary>
        /// 用户验证
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public User CheckLogin(string userCode, string pwd)
        {
            var nvc = new NameValueCollection();
            nvc.Add("Code", userCode);
            nvc.Add("Password", pwd);
            IList list = GetObjectList((new User()).GetType().Name, nvc);
            if (list != null && list.Count == 1 && list[0] is User)
            {
                return (User)list[0];
            }
            return null;
        }

        /// <summary>
        /// 根据用户编号获取用户对象
        /// </summary>
        /// <param name="strCode">用户编号</param>
        /// <returns></returns>
        public User GetUserByCode(string strCode)
        {
            var nvc = new NameValueCollection {{"Code", strCode}};
            IList list = GetObjectList((new User()).GetType().Name, nvc);
            if (list != null && list.Count == 1 && list[0] is User)
            {
                return (User)list[0];
            }
            return null;
        }

        /// <summary>
        /// 根据用户ID获取用户对象
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public User GetUser(Guid id)
        {
            User reVal = (User)(Session.Load(new User().GetType().Name, id));
            return reVal;
        }

        /// <summary>
        /// 获取用户所拥有的资源
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public NameObjectDictionary GetUserResourceSet(string userCode)
        {
            var nd = new NameObjectDictionary();

            return nd;
        }

        public bool CheckIsAdmin(string userId)
        {
            var dt =
                  GetDataTableBySql(
                     "select Alias from dbo.Frame_Role frole join dbo.Frame_User_Role fur on frole.Id = fur.RoleId join dbo.Frame_User fu on fu.Id = fur.UserId where fu.Id ='" +
                     userId + "'");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var roleAlias = dt.Rows[i][0].ToString();
                    if (roleAlias == "admin")
                        return true;
                }
            }
            return false;
        }

        public static bool CheckDogKeys()
        {
            var dog = new Dog(100) { DogAddr = 0, DogBytes = 10 };
            var r = new Random(1000);
            int Retcode = dog.ReadDog();
            if (Retcode == 0)
            {
                return true;
            }
             return false;
            
        }
    }
}